#include "Socket.hpp"
#include "protocol.hpp"
#include "TcpServer.hpp"
#include "NetCal.hpp"
#include "Daemon.hpp"

int main(int argc, char* argv[])
{
    if(argc != 2)
    {
        std::cerr << "Usage: " << argv[0] << "port" << std::endl;
        exit((int)exit_code::USAGE_ERR);
    }

    //守护进程化
    Daemon();

    //日志显示到文件
    Enable_File_Stratrgy();

    //应用层 -- 只负责处理计算业务
    std::unique_ptr<Cal> cal = std::make_unique<Cal>();

    //表示层 -- 只负责解析和包装报文
    std::unique_ptr<Protocol> protocol = std::make_unique<Protocol>([&cal](const Request& req)
    {
        return cal->Execute(req);
    });

    //会话层 -- 只负责报文的接收和发送
    std::unique_ptr<TcpServer> tsvr = std::make_unique<TcpServer>(std::stoi(argv[1]), 
        [&protocol](std::shared_ptr<Socket>& sock, InetAddr& client)
    {
        protocol->GetRequest(sock, client);
    });

    tsvr->Start();
    return 0;
}